<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="paryorder-content">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" :auto="false">
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="payorder-container u-relative" v-for="(item,index) in dataList" :key="index" @click="itemClick(item)">
				<view class="payorder-header">
					<view class="u-flex u-flex-1">
						<image :src="item.url==null?defaultLogo:item.url" mode="" class="section-image"></image>
						<view class="u-flex-1 u-font-28 u-m-l-10">{{item.sellerShopName}}</view>
					</view>
					<view class="u-flex">
						<text class="u-font-26 u-font-bold u-theme-main u-m-l-10" v-if="item.state === 1">待开票</text>
						<text class="u-font-26 u-font-bold u-theme-main u-m-l-10" v-if="item.state === 3">已开票</text>
						<text class="u-font-26 u-font-bold u-theme-main u-m-l-10" v-if="item.state === -1">已取消</text>
					</view>
				</view>
				<view class="payorder-section">
					<view class="section-list">
						<view class="section-item">
							<text class="u-font-24 u-font-grey">订单明细：</text>
							<text style="font-size: 24rpx; color: #999;">订单数量：{{item.orderQuantity}}  /  明细数量：{{item.itemQuantity}}</text>
						</view>
						<view class="section-item u-row-right u-m-t-16" >
							<text style="font-size: 26rpx;font-weight: bold; color: #FF551A;">实付款：￥{{item.actualAmount}}</text>
						</view>
					</view>
				</view>
				<view class="footer u-flex u-row-between">
					<text style="font-size: 26rpx;font-weight: bold; color: #999;">{{$fui.dateFormatter(item.createdTime,'y-m-d h:i')}}</text>
					<fui-button text="查看" v-if="item.paymentStatus != 5"  @click="itemClick(item)"
					height="54rpx" width="150rpx" radius="45rpx" size="24" background="#FFECBF" color="#FFBB1A"></fui-button>
				</view>
				<view v-if="item.state == 3" style="position: absolute;bottom: 80rpx;left: 50%;margin-left: -64rpx;">
					<image src="/static/common/kaipiaowancheng.png" style="width: 127rpx; height:113rpx;"></image>
				</view>
			</view>
			
		</z-paging>
		
	</view>
</template>

<script>
	import {useUserStore} from '@/store/user.js';
	import {useShopStore} from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		data() {
			return {
				dataList: [],
				paymentMethod:{
					0:'微信支付',
					1:'支付宝支付',
					10:'工行支付',
					3:'线下支付',
					4:'线下支付'
				},
				firstLoaded: false,
				paymentStatus:['待付款','待到账','已完成','部分完成','支付失败','已关闭','退款中','已退款',]
			}
		},
		props:{
			//当前组件的index，也就是当前组件是swiper中的第几个
			tabIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
			//当前swiper切换到第几个index
			currentIndex: {
				type: Number,
				default: function(){
					return 0
				}
			},
			filterData:{
				type:Object,
				default:function(){
					return {
						startTime:'',
						endTime:'',
						saleShopName:'',
						keywords:"",
						minTotal:'',
						maxTotal:'',
					}
				}
			},
			reload:{
				type: Boolean,
				default: function(){
					return false
				}
			}
		},
		watch: {
			currentIndex: {
				handler(newVal) {
					if(newVal === this.tabIndex){
						//懒加载，当滑动到当前的item时，才去加载
						if(!this.firstLoaded){
							setTimeout(() => {
								this.$refs.paging.reload();
							}, 100);
						}
					}
				},
				immediate: true
			},
			filterData: {
				handler(newVal) {
					if(this.reload){
						setTimeout(() => {
							this.$refs.paging.reload();
						}, 100);
					}
				},
				deep:true,
				immediate: true
			},
		},
		computed:{
			...mapState(useShopStore, {
				storeShop: 'shop',
			})
		},
		methods: {
			async queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					pageIndex: pageNo,
					pageSize: pageSize,
					shopId:this.storeShop.id,
				}
				if(this.currentIndex === 1){
					params.InvoiceStates = 1
				}
				if(this.currentIndex === 2){
					params.InvoiceStates = 3
				}
				if(this.currentIndex === 3){
					params.InvoiceStates = -1
				}
				if(this.filterData.startTime!==''){
					params.CreatedTimeStart = this.filterData.startTime;
				}
				if(this.filterData.endTime!==''){
					params.CreatedTimeEnd = this.filterData.endTime;
				}
				if(this.filterData.keywords!==''){
					params.Keyword = this.filterData.keywords
				};
				if(this.filterData.minTotal!==''){
					params.minAmount = this.filterData.minTotal
				};
				if(this.filterData.maxTotal!==''){
					params.maxAmount = this.filterData.maxTotal
				};
				uni.showLoading({
					title: 'loading...',
					mask: false
				});
				const res = await this.$newHttp.get('api/v1.3/Invoice/BuyerGetInvoicesPage', { params: params } );
				uni.hideLoading({
					title: 'loading...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
					this.firstLoaded = true;
					
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				if(item.paymentStatus != 5){
					this.$mRouter.push({
						route: '/pages/purchase/invoiceOrder/details?id='+item.id,
					})
				}else{
					uni.$fui.toast('该发票单已关闭')
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.paryorder-content {
		height: 100%;
		background-color: #f8f8f8;
		padding: 30rpx;
	}
	.payorder-container{
		background-color: #fff;
		box-sizing: border-box;
		padding: 30rpx;
		margin-bottom: 20rpx;
		.payorder-header{
			display: flex;
			justify-content: space-between;
			.section-image{
				width: 45rpx;
				height: 45rpx;
				border-radius: 50%;
			}
		}
		.payorder-section{
			display: flex;
			box-sizing: border-box;
			padding: 30rpx 0;
			border-bottom: solid 1px #eaeaea;
			margin: 0rpx 0 30rpx;
			
			.section-list{
				flex: 1;
				margin-left: 20rpx;
				.section-item{
					display: flex;
					width: 100%;
					// justify-content: space-between;
					.section-item-name{
						width: 300rpx;
						overflow: hidden;
						text-overflow:ellipsis;
						white-space:nowrap;
					}
				}
			}
			
		}
		
	}


</style>
